//
//  SlideTransitionAnimator.swift
//  Animator
//
//  Created by liu guoqiang on 16/9/11.
//  Copyright © 2016年 liu guoqiang. All rights reserved.
//

import UIKit

class SlideTransitionAnimator: NSObject,UIViewControllerAnimatedTransitioning {

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let transformedStartFrame = toVC!.view.frame
        
        let origin = transformedStartFrame.origin
        let width = transformedStartFrame.width
        let height = transformedStartFrame.height
        let transformedEndframe = CGRectMake((origin.x)-width, origin.y,width, height)
        transitionContext.containerView()?.addSubview((fromVC?.view)!)
        transitionContext.containerView()?.addSubview((toVC?.view)!)
        
        UIView.animateWithDuration(3, animations: {
            toVC?.view.frame = transformedEndframe
            toVC?.view.alpha = 0.5
            }) { (finished) in
                toVC?.view.frame = transformedEndframe
                toVC?.view.alpha = 1.0
                transitionContext.completeTransition(true)
        }
    }
}
